/*
 * Copyright (c) 2017 22420103@QQ.COM 版权所有
 */
package com.lehe.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lehe.common.exceptions.LHAdminException;
import com.lehe.dao.AdminRoleDao;
import com.lehe.dao.GameUserDao;
import com.lehe.dao.XHUserListDao;
import com.lehe.entry.AdminRoleDto;
import com.lehe.entry.AdminUserDto;
import com.lehe.entry.BaseResponse;
import com.lehe.entry.dbbean.GameUserDto;
import com.lehe.entry.xh.UserDto;

/**
 * 用户账户操作
 *
 * @author 王丹
 * @since 2017-02-26
 */
@Transactional
@Service
public class XHAddUserService extends AbstractService {

    @Autowired
    private GameUserDao gameUserDao;
	
	@Autowired
    private XHUserListDao xHUserListDao;
	
    @Autowired
    private AdminUserService adminUserService;
    
    @Autowired
	private AdminRoleDao adminRoleDao;
    
    @Autowired
    private AdminRoleService adminRoleService;

    /**
     * 新增一个运营号,也就是角色为运营号的AdminUser记录
     * @param name 姓名
     * @param account 账号
     * @param pwd 密码
     * @param phone 电话
     * @param persent 提成比例
     * @throws LHAdminException 
     */
	public BaseResponse addTopUser(String name, String account, String pwd, String phone, Integer persent) throws LHAdminException {
		
		
		if(persent == null ){
			throw new LHAdminException("添加运营号必须填写提成比例");
		}
		
		
		//添加AdminUser表记录，填充相应的字段，设置角色为运营号
		AdminUserDto dto = new AdminUserDto();
		dto.setAccount(account);
		dto.setMobile(phone);
		dto.setPwd(pwd);
		dto.setName(name);
		dto.setUserIds("0");
		AdminRoleDto role = adminRoleService.queryByName("运营号");
		if(role == null){
			throw new LHAdminException("运营号角色不存在，请联系管理员添加运营号角色");
		}
		dto.setRoleId(role.getId());
		dto.setPersent(persent);
		dto.setScore(0);
		BaseResponse response = adminUserService.insertAdminUser(dto);
		return response;
	}

	/**
	 * 为一个运营号添加下级
	 * @param topId 运营号,如果是运营号角色登陆，则从session中获取，如果是超级管理员登陆，则从界面传入
	 * @param parentId 父级ID必须属于该运营号并且utype=1，如果为空则父级为空，视为第一级代理
	 * @param userId 要添加的userId
	 * @param utype 0普通玩家1代理
	 * @param oprId 当前登陆adminUserId
	 */
	public BaseResponse addSubUser(Long parentId, Long userId, Integer utype,Long topId,Long oprId) {
		BaseResponse baseResponse = new BaseResponse(BaseResponse.FAIL, "保存失败");
		if(topId == null){
			baseResponse.setMsg("请传入运营号信息，或以运营号角色登陆");
			return baseResponse;
		}
		
		AdminUserDto adminUserDto = adminUserService.queryById(topId);
		AdminRoleDto roleDto = adminRoleDao.queryById(adminUserDto.getRoleId());
        if(roleDto == null || !roleDto.getName().equals("运营号")){
        	baseResponse.setMsg("请传入运营号不存在或不是运营号角色");
			return baseResponse;
        }
		
		if(userId == null){
			baseResponse.setMsg("请传入要添加的用户id");
			return baseResponse;
		}
		
		List<UserDto> result = xHUserListDao.queryUserList(null,null,userId,null,null,null,null,null,null,null);
		if(result == null || result.isEmpty()){
			baseResponse.setMsg("设置的用户ID不存在");
			return baseResponse;
		}
		
		UserDto u = result.get(0);
		if(u.getTopId() != null){
			baseResponse.setMsg("该玩家已经添加过了");
			return baseResponse;
		}
		
		if(u.getParentId() != null){
			baseResponse.setMsg("该玩家存在上级");
			return baseResponse;
		}
		
		if(parentId == null && utype.equals(0)){
			baseResponse.setMsg("不指定父级相当于顶级代理，必须选择为代理玩家");
			return baseResponse;
		}
		
		if(parentId != null){
			List<UserDto> pr = xHUserListDao.queryUserList(null,null,parentId,null,null,null,null,null,null,null);
			if(pr.isEmpty()){
				baseResponse.setMsg("父级"+parentId+"不存在");
				return baseResponse;
			}
			
			if(pr.get(0).getTopId() == null){
				baseResponse.setMsg("父级"+parentId+"还没添加到运营号");
				return baseResponse;
			}
			
			if(!pr.get(0).getTopId().equals(topId)){
				baseResponse.setMsg("父级所属运营号与本次添加账号的运营号不一致");
				return baseResponse;
			}
		}
		
		GameUserDto gameUserDto = new GameUserDto();
		gameUserDto.setCrtDate(new Date());
		gameUserDto.setCrtOpr(oprId);
		gameUserDto.setParentId(parentId);
		gameUserDto.setTopId(topId);
		gameUserDto.setUserId(userId);
		gameUserDto.setUtype(utype);
		
		gameUserDao.insert(gameUserDto);
		
		baseResponse.setCode(BaseResponse.SUCCESS);
		baseResponse.setMsg("成功");
		return baseResponse;
	}

}
